﻿<!--
 *  12306 Booking Helper Extension for Google Chrome and Chrome-base browsers such as 360chrome.
 *  Copyright (C) 2012 Landman
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
-->
<!DOCTYPE HTML>
<html>
  <head>
  <meta charset=utf-8>
  <title></title>
  <script type="text/javascript">

    if (typeof(PIAO)=="undefined") PIAO = {};
    
    var gTargetHomepage = "http://chrome.360.cn/extjump/jump12306.html";//"https://dynamic.12306.cn/otsweb/loginAction.do";
    (function(){
      //init
      window.addEventListener('load',function() {
        PIAO.plugin=document.getElementById('pluginId');
        chrome.browserAction.setIcon({ path: "piao.png" });
        
        PIAO.iconUrl = chrome.extension.getURL("12306.png");
        if (chrome.management.downloadHostMapRule
            && chrome.management.switchHostDNSRand) {
          PIAO.switch_config_download = false;
          PIAO.can_switch = true;
        }
      },false);
      
      // message from content script
      chrome.extension.onRequest.addListener(
        function(request, sender, sendResponse) {
          if (request.active) {
            chrome.management.getAll( function(items) {
              var exist="no";
              for(var i in items) {
                if(request.active== items[i].id) {
                  if(items[i].enabled) exist="yes";
                  break;
                }
              }
              sendResponse({ret: exist});
            });
          } else if (request.switchcdn) {
            var bSuccess=false;
            if (PIAO.can_switch==true) {
              if (request.switchcdn == "query") {
                bSuccess = true;
              } else if (request.switchcdn == "now") {
                // query the hmap config if needed
                if (PIAO.switch_config_download == false) {
                  setTimeout(function() {
                    chrome.management.downloadHostMapRule();
                  }, 100);
                  PIAO.switch_config_download = true;
                }
                // do the switch in 10s
                setTimeout(function() {
                  chrome.management.switchHostDNSRand();
                }, 10000);
                bSuccess = true;
              }
            }
            sendResponse({ret:bSuccess?"yes":"no"});
          } else if (request.notify) {
            var notification = webkitNotifications.createNotification(
              PIAO.iconUrl,  // icon url - can be relative
              '火车票刷票机',  // notification title
              request.notify
            );
            notification.show();
          }
      });
      
    })();
    
    function goToUrl(targetUrl) {
      if(targetUrl==undefined) return;
      chrome.tabs.getSelected(undefined, function(tab) {
        if (tab && tab.url && !tab.incognito && tab.url.indexOf("chrome://newtab")==0) {
          chrome.tabs.update(tab.id, {url: targetUrl});
        } else {
          chrome.tabs.create({ url: targetUrl });
        }
      });
    }
    
    function isInstalled() {
      var ret = false;
      if(PIAO.plugin && PIAO.plugin.IsInstalled)
        ret = PIAO.plugin.IsInstalled();
      if(ret) goToUrl(gTargetHomepage);
      return ret;
    }
    
    function installCert() {
      var ret = false;
      if(PIAO.plugin && PIAO.plugin.Install)
        ret = PIAO.plugin.Install();
      if(ret) goToUrl(gTargetHomepage);
      return ret;
    }

  </script>
  </head>
  
<body>
<embed id="pluginId" type="application/x-np-piao"></embed>
</body>
</html>

